<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>歌手管理</el-breadcrumb-item>
      <el-breadcrumb-item>歌手列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!--搜索区域-->
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @keydown.enter.native="getSinger"
                    class="input-with-select" @clear="getSinger">
            <el-button slot="append" icon="el-icon-search" @click="getSinger"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openDialog">新增歌手</el-button>
        </el-col>
      </el-row>
      <!--显示区域-->
      <el-row>
        <el-table :data="singers" border style="width: 100%" stripe @expand-change="expandto"
                  :row-key="getRowKeys" :expand-row-keys="expands">
          <el-table-column type="expand" width="100px">
            <template slot-scope="props">
              <el-row>
                <el-col :span="6">
                  <img style="height: 150px;" :src="props.row.photo">
                </el-col>
                <el-col :span="6">
                  <p class="song-name">歌手姓名：{{props.row.name}}</p>
                </el-col>
              </el-row>
              <el-row class="song-row">
                <el-col :span="6">
                  <span>歌手热度：{{props.row.hot}}</span>
                </el-col>
              </el-row>
              <el-row class="song-row">
                歌手简介：{{props.row.introduce}}
              </el-row>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="歌手"></el-table-column>
          <el-table-column prop="hot" label="热度"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="props">
              <el-upload class="upload-demo" :action="upImgURL"
                         :show-file-list="false" :on-success="uploadSuccess">
                <el-button size="small" plain :type="props.row.photo !== '' ? 'success' : 'primary'" @click="updateImg.id = props.row.id">
                  {{props.row.photo !== '' ? '修改图片' : '上传图片'}}
                </el-button>
              </el-upload>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="openDialog(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="deleteSinger(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
        <!--分页区域-->
        <el-row>
          <el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange"
                         :total="total">
          </el-pagination>
      </el-row>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible"
               :close-on-click-modal="false" @close="close">
      <el-form :model="singer" label-width="80px" ref="singerRef">
        <el-form-item label="歌手名">
          <el-input v-model="singer.name"></el-input>
        </el-form-item>
        <el-form-item label="歌手热度">
          <el-input-number v-model="singer.hot" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="歌手介绍">
          <el-input type="textarea" placeholder="不要超过200个字符" v-model="singer.introduce"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      upImgURL: 'http://localhost:9000/api/upload/file',
      dialogTitle: '',
      // 查询信息
      queryinfo: {
        page: 1,
        size: 10,
        query: ''
      },
      dialogFormVisible: false,
      // 歌手列表
      singers: [],
      // 总数
      total: -1,
      id: 'id',
      singer: {
        name: '',
        hot: 50,
        introduce: '',
        photo: ''
      },
      expands: [],
      getRowKeys (row) {
        return row.id
      },
      // 上传图片
      updateImg: {
        id: null,
        photo: null
      }
    }
  },
  methods: {
    // 文件上传成功的钩子
    uploadSuccess (response) {
      this.updateImg.photo = response.url
      this.$http.put(`singer/${this.updateImg.id}`, this.updateImg).then(res => {
        if (res.data.code === 10000) {
          this.$message.success({
            message: '图片上传成功',
            duration: 1000
          })
          this.updateImg = {id: null, fileType: null}
          this.getSinger()
        }
      })
    },
    // 展开行
    expandto (row, expandedRows) {
      if (expandedRows.length) {
        this.expands = []
        if (row) {
          this.expands.push(row.id)
        }
      }
      else {
        this.expands = []
      }
    },
    // 删除用户
    deleteSinger (id) {
      this.$confirm('是否删除该歌手', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确认',
        cancelButtonText: '放弃'
      }).then(() => {
        this.$http.delete(`singer/${id}`).then(res => {
          console.log(this.total % this.queryinfo.size)
          if (this.total % this.queryinfo.size === 1) {
            this.queryinfo.page = Math.floor(this.total / this.queryinfo.size)
            this.getSinger()
          } else {
            this.getSinger()
          }
          this.hite(res.data.code)
        })
      }).catch(action => {
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration: 1000
        })
      })
    },
    submitForm () {
      // 修改
      if (!isNaN(this.id)) {
        this.$http.put(`singer/${this.id}`, this.singer).then(res => {
          this.getSinger()
          this.id = 'id'
          this.hite(res.data.code)
        })
      }
      // 添加
      else {
        this.$http.post('singer', this.singer).then(res => {
          this.getSinger()
          this.hite(res.data.code)
        })
      }
      this.dialogFormVisible = false
    },
    // 打开dialog对话框
    openDialog (id) {
      if (!isNaN(id)) {
        this.id = id
        this.dialogTitle = '修改歌手'
        this.$http.get(`singer/${id}`).then(res => {
          this.singer = res.data.data
        })
      } else {
        this.dialogTitle = '新增歌手'
      }
      this.dialogFormVisible = true
    },
    // 关闭dialog对话框
    close () {
      this.$refs.singerRef.resetFields()
      this.singer = { name: '', hot: 50, introduce: '', photo: '' }
    },
    // 页码改变
    handleCurrentChange (val) {
      this.queryinfo.page = val
      this.getSinger()
    },
    getSinger () {
      this.$http.get(`singer/${this.queryinfo.page}/${this.queryinfo.size}`,
        { params: { 'query': this.queryinfo.query } }
      ).then(res => {
        this.singers = res.data.data.rows
        this.total = res.data.data.total
        // this.queryinfo.page = 1
      })
    },
    hite (code) {
      if (code === 10000) {
        this.$message.success({
          message: '成功',
          duration: 1000
        })
      } else {
        this.$message.error({
          message: '失败',
          duration: 1000
        })
      }
    }
  },
  created () {
    this.getSinger()
  }
}
</script>

<style scoped>
  .el-input-number {
    width: 100%;
  }
  .song-row{
    margin-top: 15px;
  }
</style>
